import { createRouter, createWebHistory } from "vue-router";
import MainLayout from "../layouts/MainLayout.vue";

// 自动导入pages目录下的所有vue文件作为路由
const pages = import.meta.glob("../pages/**/*.vue");

// 处理路由路径
const routes = [
  {
    path: "/",
    component: MainLayout,
    children: [
      {
        path: "",
        name: "Home",
        component: () => import("../pages/Home.vue"),
      },
      {
        path: "/about",
        name: "About",
        component: () => import("../pages/About.vue"),
      },
      {
        path: "/products-list",
        name: "ProductsList",
        component: () => import("../pages/ProductsList.vue"),
      },
      {
        path: "/production/schedule",
        name: "ProductionSchedule",
        component: () => import("../pages/ProductionSchedule.vue"),
      },
    ],
  },
  // 添加404页面
  {
    path: "/:pathMatch(.*)*",
    name: "NotFound",
    component: () => import("../pages/NotFound.vue"),
  },
];

const router = createRouter({
  history: createWebHistory(),
  routes,
});

// 路由守卫
router.beforeEach((to, from, next) => {
  // 可以在这里添加身份验证、权限控制等逻辑
  // 例如：检查用户是否已登录
  const isAuthenticated = localStorage.getItem("token");

  if (to.meta.requiresAuth && !isAuthenticated) {
    next("/login");
  } else {
    next();
  }
});

export default router;
